package part_2;

import java.text.DecimalFormat;

public class Evaluate {

	public static void main(String[] args) {
		//Config
		final int MAX_VALUES_PER_LINE = 5;
		
		//Decleration
		double[] results = new double[61];
		
		//evaluate the function
		evaluate(results);
		
		//dump the array on the screen
		dumpArray(results, MAX_VALUES_PER_LINE);
		
		// Display min/max of the function
		System.out.println();
		System.out.println("The minimum is :" + calcMin(results));
		System.out.println("The maximum is :" + calcMax(results));
	}
	
	/*
	 * Evaluate y = x^2 - 3x + 0.3
	 * for x: -3 -> 3  per 0.1
	 * 
	 * pass an array in witch the result must be placed
	 */
	public static void evaluate(double[] result){
		for(int i=0 ; i <= 60; i++) {
			result[i] = Math.pow(((i-30.)/10), 2) - (3 * ((i-30.)/10)) + 0.3;
		}
	}
	
	/*
	 * Calculate the maximum of a given array
	 * 
	 * pass array of doubles
	 * returns the back as a double
	 */
	public static double calcMax(double[] array){
		double calcMax = array[0]; 
		for (int i = 0 ; i < array.length ; i++) {
			if(array[i] > calcMax)
				calcMax = array[i];
		}
		return calcMax;
	}

	/*
	 * Calculate the minimum of a given array
	 *
	 * pass array of doubles
	 * returns the back as a double
	 */
	public static double calcMin(double[] array){
		double calcMin = array[0];
		for (int i = 0 ; i < array.length ; i++) {
			if(array[i] < calcMin)
				calcMin = array[i];
		}
		return calcMin;
	}	
	
	/*
	 * Dumps the contents of a  array on console
	 * 
	 * pass the array to be dumped
	 */
	public static void dumpArray(double[] array, int valuesPerLine){
		DecimalFormat df = new DecimalFormat("0.00");
		
		for(int i=0 ; i <= array.length ;i = i + valuesPerLine){
			for(int j=0; j <= valuesPerLine-1; j++){
				//detect end of array
				if((i+j) >= array.length )
					break;
				//output the value of x and the corresponding value of y
				System.out.print((((i+j)-30.)/10) + " = " + df.format(array[i+j]) + "   ");
			}
			//new line :)
			System.out.println();
		}
	}

}
